import serial, time

elm = serial.Serial("/dev/cua00", 38400, timeout=1)
#print elm
time.sleep(0.5)
#elm.flush()
# Echo Off
#elm.write("ATL0\r")
#elm.flush()

# On lit la crasse qui traine avant de commencer quoique ce soit
while elm.readline() <> "":
    print "nettoyage crasse"

# Echo Off
elm.write("ATE0\r")
while elm.readline() <> "":
    print "brol apres echo off"

# Linefeed Off
elm.write("ATL0\r")
while elm.readline() <> "":
    print "brol apres linefeed off"


elm.write("ATI\r")
reply1=elm.readline()
print reply1
print "@@@@@@@[",reply1,"]"
ok=""
for i in reply1:
    print "~",ord(i),i
    if ord(i) > 13 and ord(i) != 62:
	ok=ok+i
print "OK=[",ok,"]"

elm.write("0170\r")
reply2=elm.readline()
print "@@@@@@@[",reply2,"]"
ok=""
for i in reply2:
    print "~",ord(i),i
    if ord(i) > 13 and ord(i) != 62:
	ok=ok+i
print "OK=[",ok,"]"


pids=open("PIDS","w")
for i in range(1,255):
    hex = "%02X" % i
    strhex = "01"+hex
    #print strhex
    elm.write(strhex+"\r")
    reply2=elm.readline()
    ok=""
    for i in reply2:
	if ord(i) > 13 and ord(i) != 62:
	    ok=ok+i
    pidtested=strhex+"->"+ok
    print pidtested
    pids.write(pidtested+"\n")

pids.close()
    
    
#print "->ATI=[",elm.readline(),"|",elm.readline(),"]"
#print "ATI",elm.readline()
#elm.write("0170\n\r")
#print "0101",elm.readline()

elm.close()
print "done"

